package com.guosheng.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guosheng.dao.SysCardInfoMapper;
import com.guosheng.pojo.SysCardInfo;
import com.guosheng.pojo.SysCardInfoExample;
import com.guosheng.service.SysCardService;
@Service
public class SysCardServiceImpl implements SysCardService {

	@Autowired
	SysCardInfoMapper sysCardInfoMapper;
	
	@Override
	public int insertAccount(SysCardInfo sysCardInfo) {
		//获取到用户的id，然后随机生成卡号，生成开户日期，设置成冻结的状态，存款为0
		/*
		 *19位的银联卡其中1-6位代表发卡行，7-10位代表发卡地区，
		 *第11位代表卡种类，第12-18位为发卡顺序号，第19位为校验位.
		*/
		sysCardInfo.setCardid(new Random().nextInt(900000)+100000
				+""+new Random().nextInt(9000000)+100000);
		sysCardInfo.setOpendate(new Date());
		sysCardInfo.setAvailabel(SysCardInfo.CARD_STATUS_FREEZE);
		sysCardInfo.setOpenmoney(0L);
		return sysCardInfoMapper.insert(sysCardInfo);
	}

	@Override
	public int freezeAccount(String cardId) {
		SysCardInfoExample e=new SysCardInfoExample();
		SysCardInfoExample.Criteria criteria= e.createCriteria();
		criteria.andCardidEqualTo(cardId);
		SysCardInfo  sysCardInfo=sysCardInfoMapper.selectByExample(e).get(0);
		if(sysCardInfo.getAvailabel()==SysCardInfo.CARD_STATUS_FREEZE){
			sysCardInfo.setAvailabel(SysCardInfo.CARD_STATUS_RELEASE);
		}else{
			sysCardInfo.setAvailabel(SysCardInfo.CARD_STATUS_FREEZE);
		}
		return sysCardInfoMapper.updateByPrimaryKey(sysCardInfo);
	}

	@Override
	public int realseAccount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<SysCardInfo> findByUserId(String customerId) {
		SysCardInfoExample e=new SysCardInfoExample();
		SysCardInfoExample.Criteria criteria= e.createCriteria();
		criteria.andCustomeridEqualTo(customerId);
		List<SysCardInfo> list=sysCardInfoMapper.selectByExample(e);
		return list;
	}
	

}
